/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.general.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

/**
 *
 * @author Gilmar Montoya
 */
public class ExportadorHTML implements FactoriaJRExporterInterfaz{

    public ExportadorHTML() {
    }

    @Override
    public JRExporter exportar(ListaSerializada<JasperPrint> jasperPrintList, HttpServletResponse response) throws IOException {
        JRExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
        setSessionProperty(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrintList);
        Map imagesMap = new HashMap();
        Map fontMap = new HashMap();
        setSessionProperty("IMAGES_MAP", imagesMap);
        exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
        exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);
        exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "px");
        exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        exporter.setParameter(JRHtmlExporterParameter.FONT_MAP, fontMap);
        return exporter;
    }

    @Override
    public String getMimeType() {
        return "text/html";
    }

    @Override
    public String getFileExtension() {
        return "html";
    }

    /**
     * Establece el valor a una propiedad de HttpSession
     */
    private static synchronized void setSessionProperty(String key, Object value) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().put(key, value);
    }
}
